package View.LogDialog;

import View.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LogDialog extends JDialog implements ActionListener {
    MainFrame parentFrame;
    JTextField jtfRad = new JTextField();
    JTextField jtfSigma = new JTextField();
    JButton jbtnOK = new JButton("OK");
    JButton jbtnCancel = new JButton("Cancel");
    private int r;
    private float s;


    boolean closeFlag;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";



    public LogDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(250, 170);
        setResizable(false);
        setTitle("LoG filter parameters");
        setModal(true);
        setVisible(false);



        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);



        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);

        JLabel jlSigma = new JLabel("Set sigma (dispersion): ");
        JLabel jlRad = new JLabel("Set radius: ");

        gb.setConstraints(jlRad, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlRad);
        gb.setConstraints(jtfRad, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfRad);

        gb.setConstraints(jlSigma, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlSigma);
        gb.setConstraints(jtfSigma, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfSigma);

        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return  closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            boolean errFlag = false;
            try
            {
                r = Integer.valueOf(jtfRad.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                s = Float.valueOf(jtfSigma.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            if (errFlag) {
                JOptionPane.showMessageDialog(this, "Error parameters.", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (r <= 0) {
                JOptionPane.showMessageDialog(this, "Radius must be positive", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }

            closeFlag = true;
            setVisible(false);
        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }
        if (e.getActionCommand().equals("ONE")) {
            r = 1;
        }
        if (e.getActionCommand().equals("TWO")) {
            r = 2;
        }
        if (e.getActionCommand().equals("THREE")) {
            r = 3;
        }


    }

    public Float getSigma() {
        return s;
    }
    
    public int getR() {
        return r;
    }
}